<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>班级管理</title>
    <script src="lib/js/jquery-3.7.1.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
        }

        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 20px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        .form-group input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        .btn-group {
            display: flex;
            gap: 10px;
            justify-content: flex-end;
        }

        button {
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }

        .btn-primary {
            background-color: #007bff;
            color: white;
        }

        .btn-warning {
            background-color: #ffc107;
            color: #212529;
        }

        .btn-danger {
            background-color: #dc3545;
            color: white;
        }

        .btn-success {
            background-color: #28a745;
            color: white;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            background-color: white;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }

        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        th {
            background-color: #f8f9fa;
            font-weight: bold;
        }

        tr:hover {
            background-color: #f5f5f5;
        }

        .action-buttons {
            display: flex;
            gap: 5px;
        }

        .message {
            padding: 10px;
            margin: 10px 0;
            border-radius: 4px;
        }

        .message.success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .message.error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .hidden {
            display: none;
        }

        /* 弹窗样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
        }

        .modal-content {
            background-color: white;
            margin: 10% auto;
            padding: 20px;
            border-radius: 5px;
            width: 500px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.2);
        }

        .modal-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 1px solid #eee;
        }

        .modal-header h2 {
            margin: 0;
        }

        .close {
            color: #aaa;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
        }

        .close:hover {
            color: black;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>班级管理</h1>

    <!-- 消息提示 -->
    <div id="message" class="message hidden"></div>

    <!-- 控制按钮 -->
    <div style="text-align: right; margin-bottom: 10px;">
        <button id="add-class-btn" class="btn-primary">添加班级</button>
        <button id="refresh-btn" class="btn-success">刷新列表</button>
    </div>

    <!-- 班级列表 -->
    <div>
        <table id="class-table">
            <thead>
            <tr>
                <th>ID</th>
                <th>班级名称</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody id="class-list">
            <!-- 班级数据将通过JavaScript动态加载 -->
            </tbody>
        </table>
    </div>
</div>

<!-- 弹窗表单 -->
<div id="class-modal" class="modal">
    <div class="modal-content">
        <div class="modal-header">
            <h2 id="modal-title">新增班级</h2>
            <span class="close">&times;</span>
        </div>
        <form id="class-form">
            <input type="hidden" id="clazzId">
            <div class="form-group">
                <label for="clazzName">班级名称:</label>
                <input type="text" id="clazzName" required>
            </div>
            <div class="btn-group">
                <button type="submit" class="btn-primary">保存</button>
                <button type="button" id="modal-cancel-btn" class="btn-warning">取消</button>
            </div>
        </form>
    </div>
</div>

<script>
    $(document).ready(function() {
        // 页面加载时获取班级列表
        loadClassList();

        // 绑定添加班级按钮事件
        $('#add-class-btn').on('click', function() {
            openModal();
            resetForm();
        });

        // 绑定表单提交事件
        $('#class-form').on('submit', function(e) {
            e.preventDefault();
            saveClass();
        });

        // 绑定取消按钮事件
        $('#modal-cancel-btn').on('click', function() {
            closeModal();
        });

        // 绑定关闭按钮事件
        $('.close').on('click', function() {
            closeModal();
        });

        // 点击弹窗外部关闭弹窗
        $('#class-modal').on('click', function(e) {
            if (e.target === this) {
                closeModal();
            }
        });

        // 绑定刷新按钮事件
        $('#refresh-btn').on('click', function() {
            loadClassList();
        });
    });

    // 打开弹窗
    function openModal() {
        $('#class-modal').css('display', 'block');
    }

    // 关闭弹窗
    function closeModal() {
        $('#class-modal').css('display', 'none');
    }

    // 加载班级列表
    function loadClassList() {
        $.ajax({
            url: '/clazz/list',
            type: 'GET',
            success: function(response) {
                if (response.code === 200) {
                    renderClassList(response.data);
                } else {
                    showMessage('获取班级列表失败: ' + response.msg, 'error');
                }
            },
            error: function() {
                showMessage('获取班级列表失败，请稍后重试', 'error');
            }
        });
    }

    // 渲染班级列表
    function renderClassList(classes) {
        const tbody = $('#class-list');
        tbody.empty();

        if (classes && classes.length > 0) {
            classes.forEach(function(clazz) {
                const row = `
                        <tr>
                            <td>${clazz.clazzId || ''}</td>
                            <td>${clazz.clazzName || ''}</td>
                            <td class="action-buttons">
                                <button class="btn-primary" onclick="editClass(${JSON.stringify(clazz).replace(/"/g, '&quot;')})">编辑</button>
                                <button class="btn-danger" onclick="deleteClass(${clazz.clazzId})">删除</button>
                            </td>
                        </tr>
                    `;
                tbody.append(row);
            });
        } else {
            tbody.append('<tr><td colspan="4" style="text-align: center;">暂无班级数据</td></tr>');
        }
    }

    // 保存班级（新增或更新）
    function saveClass() {
        const clazzId = $('#clazzId').val();
        const clazzName = $('#clazzName').val();
        const clazzDesc = $('#clazzDesc').val();

        if (!clazzName) {
            showMessage('班级名称不能为空', 'error');
            return;
        }

        const data = {
            clazzName: clazzName,
            clazzDesc: clazzDesc
        };

        // 如果有clazzId，则为更新操作
        const url = clazzId ? '/clazz/update' : '/clazz/add';
        if (clazzId) {
            data.clazzId = clazzId;
        }

        $.ajax({
            url: url,
            type: 'GET',
            data: data,
            success: function(response) {
                if (response.code === 200) {
                    showMessage(clazzId ? '班级更新成功' : '班级新增成功', 'success');
                    closeModal();
                    loadClassList();
                } else {
                    showMessage((clazzId ? '班级更新失败: ' : '班级新增失败: ') + response.msg, 'error');
                }
            },
            error: function() {
                showMessage(clazzId ? '班级更新失败，请稍后重试' : '班级新增失败，请稍后重试', 'error');
            }
        });
    }

    // 编辑班级
    function editClass(clazz) {
        $('#clazzId').val(clazz.clazzId);
        $('#clazzName').val(clazz.clazzName);
        $('#clazzDesc').val(clazz.clazzDesc);
        $('#modal-title').text('编辑班级');
        openModal();
    }

    // 删除班级
    function deleteClass(clazzId) {
        if (!confirm('确定要删除这个班级吗？')) {
            return;
        }

        $.ajax({
            url: '/clazz/delete',
            type: 'GET',
            data: { clazzId: clazzId },
            success: function(response) {
                if (response.code === 200) {
                    showMessage('班级删除成功', 'success');
                    loadClassList();
                } else {
                    showMessage('班级删除失败: ' + response.msg, 'error');
                }
            },
            error: function() {
                showMessage('班级删除失败，请稍后重试', 'error');
            }
        });
    }

    // 重置表单
    function resetForm() {
        $('#class-form')[0].reset();
        $('#clazzId').val('');
        $('#modal-title').text('新增班级');
    }

    // 显示消息
    function showMessage(text, type) {
        const messageDiv = $('#message');
        messageDiv.removeClass('hidden success error').addClass(type).text(text);

        // 3秒后自动隐藏消息
        setTimeout(function() {
            messageDiv.addClass('hidden');
        }, 3000);
    }
</script>
</body>
</html>
